# This file is part of GPropertyGrid project. 
# The COPYRIGHT file at the top level of this repository 
# contains the full copyright notices and license terms.

import sys, os

try:
    DIR = os.path.dirname(os.path.realpath(__file__))
    DIR = os.path.normpath(os.path.join(DIR, '..', 'gpropertygrid'))
    if os.path.isdir(DIR):
        sys.path.insert(0, os.path.dirname(DIR))
except NameError:
    pass

from gi.repository import Gtk
from gpropertygrid.propertygrid import PropertyGrid, PropertyGridGroup
from gpropertygrid.propertystring import PropertyString
from gpropertygrid.propertycheck import PropertyCheck
from gpropertygrid.propertycolor import PropertyColor

class PropertyGridTest(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="PropertyGrid Test")
        self.set_border_width(10)
        self.set_size_request(500,500)
        
        hpaned = Gtk.Paned.new(Gtk.Orientation.HORIZONTAL)
        vpaned = Gtk.Paned.new(Gtk.Orientation.VERTICAL)
        
        frm = Gtk.Frame()
        frm.set_shadow_type(Gtk.ShadowType.IN)
        frm.add(vpaned)
        hpaned.pack1(frm, True, True)
        self.main_lable = Gtk.Label()
        hpaned.pack2(self.main_lable, True, True)

        vpaned.pack1(Gtk.Label(), True, True)

        pg = PropertyGrid("Property Grid Test")
        pg.connect("changed", self.on_change_pg)
                
        frm = Gtk.Frame()
        frm.set_shadow_type(Gtk.ShadowType.IN)
        frm.add(pg)
        vpaned.pack2(frm, True, True)   

        group1 = PropertyGridGroup("Group 1")
        pg.add_group(group1)

        group2 = PropertyGridGroup("Group 2")
        pg.add_group(group2)
        
        group1.add_property(PropertyString("String", id="1",
                    default="Default value 1", 
                    description="This is property 1 description"))

        group1.add_property(PropertyString("String read only", id="2",
                    default="Default value 2 read only!", 
                    description="This is property 2 description"))
                    
        prop2 = pg.get_property_by_id("XXX")
        if prop2 != None:
            raise ValueError("get_property_by_id() must return 'None' if property does not exist.")
        prop2 = pg.get_property_by_id("2")
        prop2.set_read_only(True)
        

        group2.add_property(PropertyCheck("Check", id="3",
                    default=None, 
                    description="This is property 3 description"))

        group2.add_property(PropertyColor("Color", id="4",
                    default="red", 
                    description="This is property 4 description"))
        
        self.add(hpaned)

    def on_change_pg(self, grid, property_):
        text = "{0} \n Property '{1}' has changed. New value: {2}".format(
                self.main_lable.get_text(),
                property_.name,
                property_.value)
        self.main_lable.set_text(text)

win = PropertyGridTest()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
